package Tasks;

import Plants.PowerPlant;
import SmartGrid.ExecutionEngine;

/**
 * Increment the electric output and send it to another power plant
 * @author jeffrey
 *
 */

public class IncrementElectricOutputAndSend extends MyRunnable{

	PowerPlant source;
	PowerPlant destination;
	int cutoff;  //level plant should charge to
	int amountToSend;  //amount of energy to send
		
	public IncrementElectricOutputAndSend(PowerPlant plant, PowerPlant destination, int cutoff, int amountToSend)
	{
		this.source = plant;
		this.destination = destination;
		this.cutoff = cutoff;
	}
	
	public IncrementElectricOutputAndSend(PowerPlant plant, PowerPlant destination, int cutoff, int amountToSend, Object lock)
	{
		this(plant, destination, cutoff, amountToSend);
		this.setLock(lock);
	}
	
	@Override
	public synchronized void run()
	{
		if(super.haltExecution()) return;
		if(source.getElectricOutput() < cutoff)
		{
			//Charge energy
			this.source.incrementElectricOutput(Tools.Options.INCREMENT_ELECTRIC_OUTPUT_INCREMENT, true);
			System.out.println("Energy level at " + source.getName() + " now at: " + source.getElectricOutput());
		}
		else
		{
			//Send the energy
			source.incrementAvailableEnergy(-amountToSend, true);  //take energy out
			//Would like to be able to model energy loss due to distance
			destination.incrementAvailableEnergy(amountToSend, true);  //send energy
			ExecutionEngine.cancelTask(this);  //remove this task from recurring tasks
		}
	}
	

}
